/**
 * Created by liubin on 2014/8/31.
 */
/**
 * 服务初始化
 */
define(['angular'], function (angular) {
    'use strict';
    return angular.module('app.services', ['ngResource', 'datatables']).
        service('baseService', ['$resource', function ($resource){
            // Restful Resource服务的基类
            this.process = function (sev, funcName, args, posts, successFunc, failureFunc, errorFunc) {
                var hasError = false;
                console.log('args');
                sev[funcName](args,
                    posts,
                    function (success) {
                        console.log(success.data);
                        if (successFunc) {
                            successFunc(success);

                        }
                    },
                    function (error) {
                        console.log('service failure');
                        hasError = true;
                        console.log(error);
                        if (failureFunc) {
                            failureFunc(error);

                        }
                    }).$promise.catch(
                    function (error) {
                        if (!hasError) {
                            console.log('service $promise catch');
                            console.log(error);
                            if (errorFunc) {
                                errorFunc(error);
                            }
                        }
                    }
                );
            }
        }]).service('alertService', function(){
            // 错误信息显示用
            this.init = function($scope){
                this.$scope = $scope;
                $scope.alerts = [];
                $scope.addAlert = function (msg) {
                    $scope.alerts.push(msg);
                };
                $scope.closeAlert = function (index) {
                    $scope.alerts.splice(index, 1);
                };
                $scope.closeAllAlert = function () {
                    $scope.alerts.splice(0, $scope.alerts.length);
                };
            }

            this.addAlert = function (msg) {
                this.$scope.addAlert(msg);
                //this.$scope.alerts.push(msg);
            };
            this.closeAlert = function(index){
                this.$scope.closeAlert(index);
                //this.$scope.alerts.splice(index, 1);
            };
            this.closeAllAlert = function () {
                //this.$scope.alerts.splice(0, this.$scope.alerts.length);
                this.$scope.closeAllAlert();
            }
        }).constant('pageSize',
            // 分页
        8)
        .factory('DTLoadingTemplate', function(){
            // datatables 的加载信息
            return { html: '<h3 class="dt-loading">加载中...</h3>' };
        }).service('spinModalService',['$modal', function($modal){
            // 更新后的遮盖层
            var modalObj = {
//                template: '<div class="modal-body">' +
//                            '<h4 class="smaller lighter grey">' +
//                            '<i class="ace-icon fa fa-spinner fa-spin orange bigger-125"></i>' +
//                            '处理中' +
//                            '</h4>'+
//                            '</div>',
                templateUrl: 'template/app/spinModal.html',
                windowClass: 'spanModal',
                backdrop: 'static',
                keyboard: false
            }
            var modalInstance;
            this.open = function(){
                console.log('open');
                modalInstance = $modal.open(modalObj);
            }
            this.close = function(){
                if(modalInstance){
                    modalInstance.close();
                    modalInstance = null;
                }
            }
        }]).
//        ervice('confirmService', ['$modal', function($modal){
//            var modalObj = {
//                templateUrl: 'template/app/confirmModal.html',
//                backdrop: 'static',
//                controller: 'modalCtrl'
//            }
//
//            /**
//             * 弹出框
//             * @param title 标题
//             * @param content 内容
//             * @param callback 回调函数
//             */
//            this.open = function(title, content, callback){
//                modalObj.resolve = {
//                    title: function () {
//                        return title;
//                    },
//                    content: function () {
//                        return content;
//                    }
//                }
//                var modalInstance = $modal.open(modalObj);
//                modalInstance.result.then(function () {
//                    console.log('ok');
//                    if(callback){
//                        callback();
//                    }
//                },
//                    function () {
//                    console.log('cancel');
//                });
//            }
//        }]).
        service('alertModalService', ['$modal', function($modal){
            /**
             * 打开模态窗口
             * @param type: 窗口类型: 'confirm', 'error'
             * @param title:标题
             * @param content: 显示内容
             * @param callback: 回调·
             */
            this.open = function(type, title, content, callback){
                var templateUrl;
                switch(type){
                    case 'confirm':
                        templateUrl = 'template/app/confirmModal.html';
                        break;
                    case 'alert':
                        templateUrl = 'template/app/alertModal.html';
                }
                var modalInstance = $modal.open({
                    templateUrl: templateUrl,
                    backdrop: 'static',
                    controller: 'modalCtrl',
                    resolve: {
                         title: function () {
                            return title;
                        },
                        content: function () {
                            return content;
                        }
                    }
                });
                modalInstance.result.then(function () {
                    console.log('ok');
                    if(callback){
                        callback();
                    }
                },
                    function () {
                    console.log('cancel');
                });
            }
        }]).constant('actionColumnTemplate',
    '<div class="hidden-sm hidden-xs action-buttons">' +
                            '<a class="blue" href="javascript:" action="view">' +
                            '<i class="ace-icon fa fa-search-plus bigger-130"></i>' +
                            '</a>' +
//                            '<a class="green" href="#">' +
//		                    '<i class="ace-icon fa fa-pencil bigger-130"></i>' +
//                            '</a>' +
                            '<a class="red" href="javascript:" action="delete">' +
                            '<i class="ace-icon fa fa-trash-o bigger-130"></i>' +
                            '</a>' +
                            '</div>' +
                            '<div class="hidden-md hidden-lg">' +
                            '<div class="inline position-relative">' +
                            '<button class="btn btn-minier btn-yellow dropdown-toggle" data-toggle="dropdown" data-position="auto">' +
                            '<i class="ace-icon fa fa-caret-down icon-only bigger-120"></i>' +
                            '</button>' +
                            '<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">' +
                            '<li>' +
                            '<a href="javascript:" class="tooltip-info" data-rel="tooltip" action="view" title="详细">' +
                            '<span class="blue">' +
                            '<i class="ace-icon fa fa-search-plus bigger-120"></i>' +
                            '</span>' +
                            '</a>' +
                            '</li>' +
//                            '<li>' +
//                            '<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">' +
//                            '<span class="green">' +
//                            '<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>' +
//                            '</span>' +
//                            '</a>' +
//                            '</li>' +
                            '<li>' +
                            '<a href="javascript:" action="delete" class="tooltip-error" data-rel="tooltip" title="删除">' +
                            '<span class="red">' +
                            '<i class="ace-icon fa fa-trash-o bigger-120"></i>' +
                            '</span>' +
                            '</a>' +
                            '</li>' +
                            '</ul>' +
                            '</div>' +
                            '</div>'
    );
});